<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>


<div id="addopponenttoseasoncontent">

<h4>Add Opponent to Season</h4>

<form>
<select class="form-control"  name="season" id="select2">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

	echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM seasons";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];
	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<?php

if (isset($_POST['season'])) {

	?>
<br><br>
Available opponents:
<br><br>
<select class="form-control"  name="">
	<option selected="selected" value="$setseason"> Show opponents already available </option>

<?php


$sql2 = "SELECT teamname 
FROM opponents
WHERE season  = '$setseason'";

$result2 = $db->query($sql2);

while ($row2 = $result2->fetch_assoc()) {
	$teamname = $row2['teamname'];

	echo "<option value=\"$teamname\"> $teamname </option>";
}

?>
</select>

<br>

<h4>Team to be Added:</h4>

<br>

<input type="text" name="teamname" class="form-control" placeholder="Team name to be added to season <?= $setseason; ?>">

<br>

<button type="submit" class="btn btn-success btn-block" id="addopponentsubmitbutton"> Add Team</button>

<?php } ?>


</form>


<script type="text/javascript">

$("#select2").on("change", function(e) {
    postData = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "./admin_addopptoseason.php",
        data: postData,
        success: function(data) { 
            $("#addopponenttoseasoncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#addopponenttoseasoncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#addopponentsubmitbutton").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./includes/handlers/admin_addopptoseason_handler.php",
        data: postData,
        success: function(data) { 
            $("#addopponenttoseasoncontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#addopponenttoseasoncontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  